package com.beiming.libra.quartz;

import com.beiming.libra.common.enums.ArbitrationCaseLogUserStatusEnum;
import com.beiming.libra.common.enums.ArbitrationCaseLogWorkStatusEnum;
import com.beiming.libra.common.enums.ArbitrationCaseStatusEnum;
import com.beiming.libra.dao.ArbitrationCaseMapper;
import com.beiming.libra.dao.ArbitrationCaseUserMapper;
import com.beiming.libra.domain.ArbitrationCase;
import com.beiming.libra.domain.ArbitrationCaseUser;
import com.beiming.libra.service.impl.ArbitrationCaseServiceImpl;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import tk.mybatis.mapper.entity.Example;

/**
 * @author zhangqi
 */
@Slf4j
public class PayCheckJob implements Job {

  @Resource
  private ArbitrationCaseMapper arbitrationCaseMapper;
  @Resource
  private ArbitrationCaseServiceImpl arbitrationCaseServiceImpl;
  @Resource
  private ArbitrationCaseUserMapper arbitrationCaseUserMapper;

  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    String id = context.getJobDetail().getKey().getName();
    log.info("案件:{} 缴费已经超期", id);
    ArbitrationCase ac = new ArbitrationCase();
    ac.setId(id);
    ac.setCaseStatus(ArbitrationCaseStatusEnum.UNPAID_FAIL.getCode());
    arbitrationCaseMapper.updateByPrimaryKeySelective(ac);

    Example acuExample = new Example(ArbitrationCaseUser.class, false);
    acuExample.createCriteria().andEqualTo("caseId", id).andEqualTo("addUserStatus", "1");
    List<ArbitrationCaseUser> acuList = arbitrationCaseUserMapper.selectByExample(acuExample);

    arbitrationCaseServiceImpl
        .addCaseLog(id, ArbitrationCaseLogUserStatusEnum.UNPAID_FAIL.getName(), "0", ArbitrationCaseStatusEnum.UNPAID_FAIL.getCode());
    arbitrationCaseServiceImpl
        .addCaseLog(id, ArbitrationCaseLogWorkStatusEnum.UNPAID_FAIL.getName(), "1", ArbitrationCaseStatusEnum.UNPAID_FAIL.getCode());
    arbitrationCaseServiceImpl
        .sendNormalMessage(id, acuList, ArbitrationCaseLogUserStatusEnum.UNPAID_FAIL.getName());
  }
}